package cn.itcast.es.service.impl;

import cn.itcast.es.dao.ESDemoRepository;
import cn.itcast.es.pojo.ESDemoBean;
import cn.itcast.es.service.IESDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * Created by bothin
 * At 2020/4/2 10:43
 * Description:
 */
@Service
public class ESDemoServiceImpl implements IESDemoService {

    @Autowired
    private ElasticsearchRestTemplate template;

    @Autowired
    private ESDemoRepository repository;

    private Pageable pageable = PageRequest.of(0,10);

    @Override
    public void createIndex() {
        this.template.createIndex(ESDemoBean.class);
    }

    @Override
    public void deleteIndex(String index) {
        this.template.deleteIndex(index);
    }

    @Override
    public void save(ESDemoBean docBean) {
        this.repository.save(docBean);
    }

    @Override
    public void saveAll(List<ESDemoBean> list) {
        this.repository.saveAll(list);
    }

    @Override
    public Iterator<ESDemoBean> findAll() {
        return this.repository.findAll().iterator();
    }

    @Override
    public Page<ESDemoBean> findByContent(String content) {
        return this.repository.findByContent(content,pageable);
    }

    @Override
    public Page<ESDemoBean> findByFirstCode(String firstCode) {
        return this.repository.findByFirstCode(firstCode,pageable);
    }

    @Override
    public Page<ESDemoBean> findBySecordCode(String secordCode) {
        return this.repository.findBySecordCode(secordCode,pageable);
    }

    @Override
    public Page<ESDemoBean> query(String key) {
        return this.findByContent(key);
    }
}
